﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuckyLoot.ServiceFacade.Contracts;
using LuckyLoot.Domain.Entities.People;
using LuckyLoot.Common.Validation;
using LuckyLoot.Common.Repositories;
using LuckyLoot.Common.Contexts;

namespace LuckyLoot.ServiceFacade.Implementation
{
    public class SupplierManagementService : ISupplierManagementService
    {
        public Supplier CreateSupplier(string name, string phone, string email, string serviceDescription, string webSite, DateTime latestPurchase)
        {
            ValidState.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(name));
            ValidState.Requires<ArgumentException>(latestPurchase.Date <= DateTime.Today);

            Supplier supplier = new Supplier();
            supplier.Name = name;
            supplier.Phone = phone;
            supplier.Email = email;
            supplier.ServiceDescription = serviceDescription;
            supplier.WebSite = webSite;
            supplier.LatestPurchase = latestPurchase;

            using (Repository<Supplier, ApplicationContext> repository = new Repository<Supplier, ApplicationContext>())
            {
                repository.Add(supplier);
                repository.Save();
            }

            return supplier;
        }

        public Supplier UpdateSupplier(int supplierID, string name, string phone, string email, string serviceDescription, string webSite, DateTime latestPurchase)
        {
            ValidState.Requires<ArgumentNullException>(supplierID > 0);
            ValidState.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(name));
            ValidState.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(phone) || !string.IsNullOrWhiteSpace(email));
            ValidState.Requires<ArgumentException>(latestPurchase.Date <= DateTime.Today);


            Supplier supplier;
            using (Repository<Supplier, ApplicationContext> repository = new Repository<Supplier, ApplicationContext>())
            {
                supplier = this.GetSupplier(supplierID);

                supplier.Name = name;
                supplier.Phone = phone;
                supplier.Email = email;
                supplier.ServiceDescription = serviceDescription;
                supplier.WebSite = webSite;
                supplier.LatestPurchase = latestPurchase;

                repository.Edit(supplier);
                repository.Save();
            }

            return supplier;
        }

        public Supplier GetSupplier(int supplierID)
        {
            ValidState.Requires<ArgumentNullException>(supplierID > 0);

            Supplier supplier = null;
            using (Repository<Supplier, ApplicationContext> repository = new Repository<Supplier, ApplicationContext>())
            {
                supplier = repository.GetById(supplierID);
                ValidState.Requires<InvalidOperationException>(supplier != null, string.Format("Não foi possível encontrar o fornecedor de id {0}.", supplierID));
            }

            return supplier;
        }

        #region ISupplierManagementService Members


        public List<Supplier> GetAllSuppliers()
        {
            var suppliers = new List<Supplier>();
            using (Repository<Supplier, ApplicationContext> repository = new Repository<Supplier, ApplicationContext>())
            {
                suppliers = repository.GetAll().ToList<Supplier>();
            }

            return suppliers;
        }

        #endregion
    }
}
